import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { getUserFromToken, setToken, unsetToken } from '../utils/auth'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    stores:[],
    routes: [],
    msgList: [],
    isDotMap: new Map(),
    currentFriend: {},
    nfDot: false
  },
  getters: {
    stores: state => {
      return state.stores
    }
  },
  mutations: {
    initMenu(state, menus){
      state.routes = menus;
    },
    initStores(state){
      axios({
        method: 'get',
        url: 'store/query/all/list'
      }).then(res => {
        state.stores = res.data;
      }).catch(error => {
        console.log(error);
      })
    },
    set_user(state,user){
      state.user = user;
      window.localStorage.setItem('user', JSON.stringify(user));
    },
    logout(state){
      window.localStorage.removeItem('user');
      state.routes = [];
      unsetToken();
    },
    toggleNFDot(state, newValue){
      state.nfDot = newValue;
    },
    updateMsgList(state, newMsgList){
      state.msgList = newMsgList;
    },
    updateCurrentFriend(state, newFriend){
      state.currentFriend = newFriend;
    },
    addValue2DotMap(state, key){
      state.isDotMap.set(key, "您有未读消息")
    },
    removeValueDotMap(state, key){
      state.isDotMap.delete(key);
    }
  },
  actions: {
  }
});
